#!/bin/bash

#安装数据库工具dbeaveree 此工具依赖java环境变量
installDbeaveree() {
    #判断如果没有javahome环境变量就安装jdk环境
    javahomePath=$(cat /home/${user}/.bash_path | grep JAVA_HOME1)
    if [ ! $? -eq 0 ]; then
        . ${currentDir}/dev/installJava.sh
    fi

    javahome=${JAVA_HOME}
    if [ -z ${javahome} ]; then
        echo "先安装java并设置JAVA_HOME环境变量"
        exit 1
    fi

    if  ! command -v git > /dev/null; then
        . ${currentDir}/dev/installAndConfigGit.sh
    fi

    if [ ! -d /home/${user}/dev/${dbeaveree_dir} ]; then
        cd /home/${user}/dev
        git clone ${git_baseurl}${dbeaveree_dir}".git"
    fi

    cd /home/${user}/dev/${dbeaveree_dir}
    echo """
-startup
plugins/org.eclipse.equinox.launcher_1.5.700.v20200207-2156.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.1100.v20190907-0426
-vm
${JAVA_HOME}/bin
-vmargs
-XX:+IgnoreUnrecognizedVMOptions
--add-modules=ALL-SYSTEM
-Xms128m
-Xmx2g
-javaagent:./dbeaver-agent/dbeaver-agent.jar
""" >/home/${user}/dev/${dbeaveree_dir}/dbeaver.ini
    sed -i "s#^Path=.*#Path=/home/${user}/dev/${dbeaveree_dir}#" /home/${user}/dev/${dbeaveree_dir}/dbeaver.desktop
    sed -i "s#^Exec=.*#Exec=/home/${user}/dev/${dbeaveree_dir}/dbeaver#" /home/${user}/dev/${dbeaveree_dir}/dbeaver.desktop
    sed -i "s#^Icon=.*#Icon=/home/${user}/dev/${dbeaveree_dir}/dbeaver.png#" /home/${user}/dev/${dbeaveree_dir}/dbeaver.desktop
    cp /home/${user}/dev/${dbeaveree_dir}/dbeaver.desktop /home/${user}/.local/share/applications
}

installDbeaveree